package com.picket.controller;

import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.picket.domain.Greeting;
import com.picket.service.IGreetingsService;

@Controller
public class GuestbookController {
	private IGreetingsService greetingsService;
	private UserService userService;
	
	@Required
	public void setGreetingsService(
			IGreetingsService greetingsService) {
		this.greetingsService = greetingsService;
	}

	@Required
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping(value = "/guestbook",
				method = RequestMethod.GET)
	public String doShowGuestbook(HttpServletRequest request,Model model)
	{
		final Collection<Greeting> greetings =
			greetingsService.getAllGreetings();
		model.addAttribute("greetings", greetings);
		model.addAttribute("signInUrl",userService.createLoginURL(request.getRequestURI()));
		return "/guestbook";
	} 
	
	@RequestMapping(value = "/guestbook", method = RequestMethod.POST)
	public String doPost(@RequestParam("content") String content,Model model)
	{
		final User user = userService.getCurrentUser();
		final Date date = new Date();
		final Greeting greeting = new Greeting(user, content, date);
		greetingsService.create(greeting);
		return "redirect:guestbook";
	}		
	
}
